#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/ide.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <linux/of.h>
#include <linux/of_address.h>



#define dtsled_CNT    1   /* 设备个数 */
#define dtsled_NAME  "dtsled" /* 名字 */

#define LEDOFF 0 /* 关灯 */
#define LEDON  1 /* 开灯 */



/* 映射后的寄存器虚拟地址指针 */
static void __iomem *CCM_CCGR1;
static void __iomem *SW_MUX_GPIO1_IO03;
static void __iomem *SW_PAD_GPIO1_IO03;
static void __iomem *GPIO1_DR;
static void __iomem *GPIO1_GDIR;

/* dtsled设备结构体 */
struct dtsled_dev
{
    dev_t devid;    /* 设备号 */
    struct cdev cdev; /* cdev */
    struct class *class; /* 类 */
    struct device *device; /* 设备 */
    int major;    /*  主设备号 */
    int minor;    /*  次设备号  */
    struct device_node *nd; /*设备节点*/

};

struct dtsled_dev dtsled;  

static int led_open(struct inode *inode, struct file *filp)
{
    filp->private_data = &dtsled;
    return 0;
}


static ssize_t led_read(struct file *filp,char __user *buf,size_t cnt, loff_t *offt)
{
    return 0;
}

static ssize_t led_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offt)
{
    int retvalue;
    u32 value;
    unsigned char databuf[1];
    unsigned char cmd;
    
    retvalue = copy_from_user(databuf,buf,cnt);
    if(retvalue < 0){
        printk("kernel write failed! \r\n");
        return -1;
    }
    cmd = databuf[0];
    if(cmd == LEDON){
        value = readl(GPIO1_DR);
        value &= ~(1 <<3); /* 亮 */
        writel(value,GPIO1_DR); 
    }

    if (cmd == LEDOFF){
    
        value = readl(GPIO1_DR);
        value |= (1 <<3); /* 灭 */
        writel(value,GPIO1_DR);
    }
    return 0;
}

static int led_release(struct inode *inode, struct file *filp)
{
    return 0;
}

static struct file_operations dtsled_fops = 
{
    .owner = THIS_MODULE, 
    .open = led_open,
    .read = led_read,
    .write = led_write,
    .release = led_release,
};

/* 驱动入口函数 
 * 初始化LED
 */
static int __init led_init(void)
{
   
    u32 val = 0;
    int ret;
    u32 regdata[14];
    const char *str;
    struct property *proper;
    printk("led start initing!!! \r\n");

    /* 获取设备树中的属性数据 */
    /* 1、获取设备节点：alphaled */
    dtsled.nd = of_find_node_by_path("/alphaled");
    if (dtsled.nd == NULL)
    {
        printk("alphaled node can not found!\r\n");
        return -EINVAL;
    }else{
        printk("alphaled node has been found!\r\n");
    }

    /* 2、获取compatible 属性内容*/
    proper = of_find_property(dtsled.nd,"compatible",NULL);
    if (proper == NULL){
        printk("compatible property find failed!\r\n");
    }else{
        printk("compatible = %s \r\n",(char*)proper->value);
    }

    /* 3、获取status属性内容 */
    ret = of_property_read_string(dtsled.nd,"status",&str);
    if (ret < 0){
        printk("status read failed!\r\n");
    }else
    {
        printk("status = %s \r\n", str);
    }

    /* 4、获取reg属性内容 */
    ret = of_property_read_u32_array(dtsled.nd,"reg",regdata,10);
    if(ret < 0) {
        printk("reg property read failed!\r\n");
    }else {
        u8 i = 0;
        printk("reg data: \r\n");
        for (i = 0; i < 10; i++)
            printk("%#X ",regdata[i]);
        printk("\r\n");
    }

    /* 初始化LED */
#if 0
    /* 地址映射 */
    CCM_CCGR1 = ioremap(CCM_CCGR1_BASE, 4);
    SW_MUX_GPIO1_IO03 = ioremap(IOMUXC_SW_MUX_GPIO1_IO03_BASE, 4);
    SW_PAD_GPIO1_IO03 = ioremap(IOMUXC_SW_PAD_GPIO1_IO03_BASE, 4);
    GPIO1_DR = ioremap(GPIO1_DR_BASE, 4);
    GPIO1_GDIR = ioremap(GPIO1_GDIR_BASE, 4);

#else
    CCM_CCGR1 = of_iomap(dtsled.nd,0);
    SW_MUX_GPIO1_IO03 = of_iomap(dtsled.nd,1);
    SW_PAD_GPIO1_IO03 = of_iomap(dtsled.nd, 2);
    GPIO1_DR = of_iomap(dtsled.nd, 3);
    GPIO1_GDIR = of_iomap(dtsled.nd, 4);
#endif

    /* 配置GPIO1相关的寄存器 */
    val = readl(CCM_CCGR1);
    val &= ~(0x03<<26);
    val |= (0x03<<26);
    writel(val, CCM_CCGR1);

    writel(5,SW_MUX_GPIO1_IO03);
    writel(0x10B0,SW_PAD_GPIO1_IO03);

    val = readl(GPIO1_GDIR);
    val &= ~(1<<3);
    val |= (1<<3);
    writel(val,GPIO1_GDIR);   

    val = readl(GPIO1_DR);
    val |= (1<<3);   /* 默认关闭 */
    writel(val,GPIO1_DR);  


    /* 注册字符设备驱动 */
    /* 1、创建设备号 */
    if (dtsled.major )
    {
        dtsled.devid = MKDEV(dtsled.major,0);
        register_chrdev_region(dtsled.devid,dtsled_CNT,dtsled_NAME);
    }else
    {
        alloc_chrdev_region(&dtsled.devid,0,dtsled_CNT,dtsled_NAME);
        dtsled.major = MAJOR(dtsled.devid);    
        dtsled.minor = MINOR(dtsled.devid);
    }
    
    printk("dtsled major = %d , minor = %d\r\n",dtsled.major,dtsled.minor);

    /* 2、初始化 */
    dtsled.cdev.owner = THIS_MODULE;
    cdev_init(&dtsled.cdev,&dtsled_fops);

    /* 3、添加一个cdev */
    cdev_add(&dtsled.cdev,dtsled.devid,dtsled_CNT);

    /* 4、创建类 */
    dtsled.class = class_create(THIS_MODULE,dtsled_NAME);
    if(IS_ERR(dtsled.class)){
        return PTR_ERR(dtsled.class);
    }

    /* 创建设备 */
    dtsled.device = device_create(dtsled.class,NULL,dtsled.devid,NULL,dtsled_NAME);

    if (IS_ERR(dtsled.device))
    {
        return PTR_ERR(dtsled.device);
    }
    
    return 0;
}

/* 驱动出口函数 */
static void __exit led_exit(void)
{
    /* 取消映射 */
    iounmap(CCM_CCGR1);
    iounmap(SW_MUX_GPIO1_IO03);
    iounmap(SW_PAD_GPIO1_IO03);
    iounmap(GPIO1_DR);
    iounmap(GPIO1_GDIR);

    /* 注销字符设备 */
    cdev_del(&dtsled.cdev);
    unregister_chrdev_region(dtsled.devid,dtsled_CNT);

    device_destroy(dtsled.class, dtsled.devid);
    class_destroy(dtsled.class);
}


module_init(led_init);
module_exit(led_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("chaoliang");





